/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package db.server;

import gui.components.basic.test.CRUDPanelTester;
import java.awt.Dialog;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.apache.derby.drda.NetworkServerControl;
import vettsspammer.VettsSpammer;

/**
 *
 * @author Luca
 */
public class DBServer {
    
    private static DBServer _instance = null;
    NetworkServerControl server = null;
    
    public static DBServer getInstance() {
        if (_instance == null) {
            _instance = new DBServer();
            return _instance;
        } else {
            return _instance;
        }
    }
    
    private DBServer() {
        super();
    }
    
    public void connect(){
          if (VettsSpammer.doDB_tests && server == null) {
            try {
                System.setProperty("derby.drda.startNetworkServer", "true");
                server = new NetworkServerControl(InetAddress.getByName("localhost"),
                        20000,
                        "root",
                        "");
                java.io.PrintWriter consoleWriter = new java.io.PrintWriter(System.out, true);
                server.start(consoleWriter);

            } catch (UnknownHostException ex) {
                Logger.getLogger(VettsSpammer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(VettsSpammer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public void disconnect(){
        if(server!=null){
            try {
                server.shutdown();
                JOptionPane.showMessageDialog(null, "Derby Server has been correctly disconnected");
            } catch (Exception ex) {
                Logger.getLogger(DBServer.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    }
    
}
